package com.se.controller;

import com.se.domain.mysql.ActivityDO;
import com.se.domain.request.param.ApplyActParam;
import com.se.domain.vo.ActInfoVO;
import com.se.domain.vo.QVO;
import com.se.domain.vo.ResultVO;
import com.se.domain.vo.UrlCodeVO;
import com.se.exception.ResultException;
import com.se.global.AuthHeaderRequestWrapper;
import com.se.service.ActApplyService;
import com.se.service.ActService;
import com.se.service.QrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
public class ActApplyController {

    @Autowired
    ActApplyService actApplyService;
    @Autowired
    ActService actService;
    @Autowired
    QrCodeService qrCodeService;
    @Value("${url.http}")
    String httpPrefix;
    @PostMapping("/must/act/info")
    public ResultVO<ActInfoVO> getActInfo(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestParam Long actId
    ) throws ResultException {
        ActInfoVO actInfoVO = actApplyService.getActFullInfo(actId, userId);

        return ResultVO.ok(actInfoVO);
    }

    @PostMapping("/must/act/info/simple")
    public ResultVO<ActInfoVO> getActSimpleInfo(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestParam Long actId
    ) throws ResultException {
        ActInfoVO actInfoVO = actApplyService.getActSimpleInfo(actId, userId);

        return ResultVO.ok(actInfoVO);
    }

    @PostMapping("/must/act/qn")
    public ResultVO<List<QVO>> getActQn(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestParam Long actId
    ) throws ResultException, ParseException {
        List<QVO> qvoList = actApplyService.getQn(actId, userId);

        return ResultVO.ok(qvoList);
    }

    @PostMapping("/must/act/apply")
    public ResultVO<Long> applyAct(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestBody @Validated ApplyActParam applyActParam
    ) throws ResultException, ParseException {
        Long applyId = actApplyService.apply(applyActParam, userId);
        return ResultVO.ok(applyId);
    }

    @PostMapping("/must/act/apply/cancel")
    public ResultVO<Void> cancelApplyAct(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestParam Long applyId
    ) throws ResultException, ParseException {
        actApplyService.cancelApplyAct(applyId, userId);
        return ResultVO.ok(null);
    }

    @PostMapping("/must/act/sign/qrcode")
    public ResultVO<UrlCodeVO> actSignQrcode(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestParam Long actId
    ) throws ResultException {
        ActivityDO activityDO = actService.checkModifyAct(actId, userId);
        String url = this.httpPrefix + "/act/sign?actId="+actId;
        String base64 = qrCodeService.base64(url);
        return ResultVO.ok(new UrlCodeVO(url, base64));
    }

    @PostMapping("/must/act/sign")
    public ResultVO<Void> signAct(
            @RequestHeader(AuthHeaderRequestWrapper.USER_ID_HEADER_NAME) Long userId,
            @RequestParam Long actId
    ) throws ResultException {
        actApplyService.sign(actId, userId);

        return ResultVO.ok(null);
    }
}
